package com.yuke.cloud.service.pmc.service.impl;

import com.yuke.cloud.common.base.enums.ErrorCodeEnum;
import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.api.omc.exceptions.OmcBizException;
import com.yuke.cloud.service.api.pmc.dto.ProdSpecInfoExDto;
import com.yuke.cloud.service.api.wmc.dto.StorageSpecInfoDto;
import com.yuke.cloud.service.api.wmc.service.WmcStorageFeignApi;
import com.yuke.cloud.service.pmc.dao.ProdAttributeMapper;
import com.yuke.cloud.service.pmc.dao.ProdSpecMapper;
import com.yuke.cloud.service.pmc.dto.ProdMainAttrDto;
import com.yuke.cloud.service.pmc.dto.ProdSpecInfoAndPriceDto;
import com.yuke.cloud.service.pmc.entity.ProdSpec;
import com.yuke.cloud.service.pmc.service.ProdCategoryService;
import com.yuke.cloud.service.pmc.service.ProdSpecService;
import com.yuke.cloud.service.pmc.service.ProdStorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by zys on 2018/09/27.
 */
@Slf4j
@Service
@Transactional
public class ProdStorageServiceImpl extends BaseService<ProdSpec> implements ProdStorageService {
    @Resource
    private ProdSpecMapper prodSpecMapper;
    @Resource
    private ProdSpecService prodSpecService;
    @Resource
    private ProdAttributeMapper prodAttributeMapper;
    @Resource
    private ProdCategoryService prodCategoryService;

    @Resource
    private WmcStorageFeignApi wmcStorageFeignApi;

    @Override
    public List<ProdSpecInfoAndPriceDto> getProdStorageByCategoryId(Long categoryId,Long shopId) {
        List<Long> leafCategoryIds = new ArrayList<>();
        prodCategoryService.leafCategoryId(categoryId, leafCategoryIds);
        if (leafCategoryIds.size() == 0) {
            log.info("--- Leave getProdByCategoryId(Long categoryId), categoryId={} is error ---", categoryId);
            return null;
        }

        List<StorageSpecInfoDto> prodProducts =  wmcStorageFeignApi.getStorageInfoByCategoryIds(leafCategoryIds,shopId);
        if (prodProducts == null && prodProducts.size() == 0) {
            log.info("--- Leave getProdByCategoryId(Long categoryId), leafCategoryIds={}分类下没有商品 ---", leafCategoryIds);
            return null;
        }

        List<Long> specIds = prodProducts.stream().map(StorageSpecInfoDto::getSpecId).collect(Collectors.toList());

        List<ProdSpecInfoAndPriceDto> list = prodSpecService.getSpecInfoDtosBySpecIds(specIds);
        if (list == null) {
            log.error("--- Leave getProdByCategoryId(Long categoryId), omcOrderFeignApi.countnum(specIds)内部异常，参数：{};返回值：{} ---", specIds,list);
            throw new OmcBizException(ErrorCodeEnum.GL99990002);
        }

        for (ProdSpecInfoAndPriceDto prodSpecInfoAndPriceDto : list) {
            for (StorageSpecInfoDto storageSpecInfoDto : prodProducts) {
                if (prodSpecInfoAndPriceDto.getSpecId().equals(storageSpecInfoDto.getSpecId())) {
                    prodSpecInfoAndPriceDto.setSalePrice(storageSpecInfoDto.getSalePrice());
                    prodSpecInfoAndPriceDto.setSaleCount(storageSpecInfoDto.getSaleCount());
                    prodSpecInfoAndPriceDto.setStockCount(storageSpecInfoDto.getStockCount());
                    prodSpecInfoAndPriceDto.setWareCityName(storageSpecInfoDto.getWareCityName());
                    prodSpecInfoAndPriceDto.setGiftSpecId(storageSpecInfoDto.getGiftSpecId());
                }
            }
        }
        prodProducts = prodProducts.stream().filter(prodSpecInfoAndPriceDto -> prodSpecInfoAndPriceDto.getSalePrice() != null).collect(Collectors.toList());
        log.info("--- Leave getProdByCategoryId(Long categoryId), 返回值大小：{} ---", prodProducts.size());

        return list;
    }

    @Override
    public ProdSpecInfoAndPriceDto getSpecByMainAttribute(ProdMainAttrDto prodMainAttrDto, Byte saleType, Long shopId) {
        //1.由specList+prodMainAttrDto筛选
        List<ProdSpecInfoAndPriceDto> list = prodAttributeMapper.getSpecInfoByAttribute(prodMainAttrDto, saleType);
        System.out.println("由specList+prodMainAttrDto筛选"+ list);
        if (list != null && list.size() > 0) {

            //2.由saleType，shopId获得specList
            Long specId = list.get(0).getSpecId();
            List<StorageSpecInfoDto> specList = wmcStorageFeignApi.getWmsSpecInfoBySpecIds(Arrays.asList(specId),shopId);

            if (specList != null && specList.size()>0){
                BeanUtils.copyProperties(specList.get(0),list.get(0));

                if (saleType.equals((byte)1)){
                    list.get(0).setSalePrice(specList.get(0).getMarketPrice());
                }else{
                    list.get(0).setSalePrice(specList.get(0).getStockPrice());
                }

            }
            return list.get(0);
        }



        return null;
    }

    @Override
    public  List<ProdSpecInfoExDto> getAppSpecInfoBySpecIds(List<Long> specIds) {
       return prodSpecMapper.getAppSpecInfoBySpecIds(specIds);
    }
}
